package com.fausto.learnimprint.core.controller.impl

import com.fausto.learnimprint.core.controller.UserModule
import com.fausto.learnimprint.core.model.dto.CommonResult
import com.fausto.learnimprint.core.model.dto.UserLogin
import com.fausto.learnimprint.core.service.IUserService
import com.fausto.learnimprint.core.service.impl.LoginServiceImpl
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 *
 *
 * 前端控制器
 *
 *
 * @author fausto
 * @since 2022-03-28
 */
@Api(tags = ["用户API"], basePath = "/users")
@RestController
@RequestMapping("/users")
class UserController : UserModule {

    @Autowired
    lateinit var userService: IUserService

    @Autowired
    lateinit var loginServiceImpl: LoginServiceImpl

    @ApiOperation("用户登录")
    @PostMapping("/login")
    override fun login(
        @ApiParam("用户信息(用户名和密码)")
        @RequestBody user: UserLogin
    ): CommonResult<Any> {
        return if (user.username.isBlank() || user.password.isBlank()) {
            CommonResult.fail("参数错误")
        } else try {
            val map = userService.login(user)
//            TODO("saveLoginInfo")
            CommonResult.ok("登陆成功", data = map)
        } catch (e: Exception) {
            CommonResult.fail("登陆失败", data = e.message)
        }
    }

    override fun logout(): CommonResult<Any> {
        return try{
            userService.logout()
            CommonResult.ok("登出成功")
        }catch (e: Exception){
            CommonResult.fail("登出失败", data = e.message)
        }

    }

//    override fun logout(): CommonResult<Any> {
//        TODO("Not yet implemented")
//    }
//
//    override fun register(user: User, mailCode: String, inviteCode: String): CommonResult<Any> {
//        TODO("Not yet implemented")
//    }
//
//    override fun ban(userId: Int, userState: Int): CommonResult<Any> {
//        TODO("Not yet implemented")
//    }
//
//    override fun updatePwd(oldPwd: String, newPwd: String, mailCode: String): CommonResult<Any> {
//        TODO("Not yet implemented")
//    }
//
//    override fun updateMail(newMail: String, newMailCode: String): CommonResult<Any> {
//        TODO("Not yet implemented")
//    }
//
//    override fun getUser(page: Int, showCount: Int): CommonResult<Any> {
//        TODO("Not yet implemented")
//    }
}